﻿using Codeplex.STSDTS.Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Codeplex.STSDTS.Tests.TestServices;
using System.Net;
using System.Configuration;

namespace Codeplex.STSDTS.Tests
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class DeploymentTest
    {

        private readonly TestService _testService;
        private readonly Farm _farm;

        public DeploymentTest()
        {
            _testService = new TestService
                               {
                                   Credentials = CredentialCache.DefaultNetworkCredentials
                               };
            //Deserialize farm file
            _farm = Farm.Deserialize(ConfigurationManager.AppSettings["FarmFile"]);
        }

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get;
            set;
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        /// <summary>
        /// Tests if the farm matches the farm file
        /// </summary>
        [TestMethod]
        public void TestFarm()
        {
            foreach (var solution in _farm.Solutions)
            {
                Assert.IsTrue(_testService.SolutionExists(solution.Id),
                    string.Format("Solution with ID {0} is not installed",
                    solution.Id));
                if (solution.WebApplications.Count == 0)
                    Assert.IsTrue(_testService.SolutionDeployedOnWebApplication(solution.Id, null),
                         "Solution with ID {0} is not deployed",
                         solution.Id);
                else
                    foreach (var webApplication in solution.WebApplications)
                    {
                        Assert.IsTrue(_testService.SolutionDeployedOnWebApplication(solution.Id, webApplication.Url),
                            "Solution with ID {0} is not installed on {1}",
                            solution.Id, webApplication.Url);
                    }
                foreach (var feature in solution.Features)
                {
                    foreach (var webApplication in feature.WebApplications)
                    {
                        Assert.IsTrue(_testService.FeatureActivatedOnWebApplication(feature.Id, webApplication.Url),
                                      "Feature with ID {0} is not activated on webapplication {1}",
                                                    solution.Id, webApplication.Url);
                    }
                    foreach (var siteCollection in feature.SiteCollections)
                    {
                        Assert.IsTrue(_testService.FeatureActivatedOnSiteCollection(feature.Id, siteCollection.Url),
                                      "Feature with ID {0} is not activated on sitecollection {1}",
                                                    solution.Id, siteCollection.Url);
                    }
                    foreach (var site in feature.Sites)
                    {
                        Assert.IsTrue(_testService.FeatureActivatedOnSite(feature.Id, site.Url),
                                      "Feature with ID {0} is not activated on site {1}",
                                                    solution.Id, site.Url);
                    }
                }
            }
        }

    }
}
